package com.mmg.cosplay.controller;

import com.mmg.cosplay.entity.R;
import com.mmg.cosplay.entity.po.KnowledgeDocument;
import com.mmg.cosplay.service.IKnowledgeService;
import dev.langchain4j.data.segment.TextSegment;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 知识库管理控制器
 * 
 * @author fly
 * @since 2025-9-24
 */
@Tag(name = "知识库管理接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {

    private final IKnowledgeService knowledgeService;

    /**
     * 添加文本知识
     */
    @Operation(summary = "添加文本知识")
    @PostMapping("/text")
    public ResponseEntity<Long> addTextKnowledge(
            @RequestParam("roleId") Long roleId,
            @RequestParam("title") String title,
            @RequestParam("content") String content) {
        Long documentId = knowledgeService.addTextKnowledge(roleId, title, content);
        return R.success(documentId);
    }

    /**
     * 上传文件知识
     */
    @Operation(summary = "上传文件知识")
    @PostMapping("/file")
    public ResponseEntity<Long> addFileKnowledge(
            @RequestParam ("roleId") Long roleId,
            @RequestParam("file") MultipartFile file) {
        Long documentId = knowledgeService.addFileKnowledge(roleId, file);
        return R.success(documentId);
    }

    /**
     * 删除知识文档
     */
    @Operation(summary = "删除知识文档")
    @DeleteMapping("/{documentId}")
    public ResponseEntity<Void> deleteKnowledge(
            @PathVariable("documentId") Long documentId,
            @RequestParam("roleId") Long roleId) {
        knowledgeService.deleteKnowledge(documentId, roleId);
        return R.success(null);
    }

    /**
     * 获取角色的知识文档列表
     */
    @Operation(summary = "获取角色的知识文档列表")
    @GetMapping("/role/{roleId}")
    public ResponseEntity<List<KnowledgeDocument>> getKnowledgeByRole(@PathVariable("roleId") Long roleId) {
        List<KnowledgeDocument> documents = knowledgeService.getKnowledgeByRole(roleId);
        return R.success(documents);
    }

    /**
     * 搜索知识库
     */
    @Operation(summary = "搜索知识库")
    @GetMapping("/search")
    public ResponseEntity<List<TextSegment>> searchKnowledge(
            @RequestParam("roleId") Long roleId,
            @RequestParam("query") String query,
            @RequestParam(value = "maxResults", defaultValue = "5") int maxResults) {
        List<TextSegment> results = knowledgeService.searchKnowledge(roleId, query, maxResults);
        return R.success(results);
    }

    /**
     * 清空角色知识库
     */
    @Operation(summary = "清空角色知识库")
    @DeleteMapping("/role/{roleId}/clear")
    public ResponseEntity<Void> clearRoleKnowledge(@PathVariable("roleId") Long roleId) {
        knowledgeService.clearRoleKnowledge(roleId);
        return R.success(null);
    }
}